/**Invoice GUI Editor
 * 
 * @author Loc Phan
 * @version 1.0
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class InvoiceEditor 
{
   // Constants
   // Size of the frame
   private static final int FRAME_WIDTH = 450; 
   private static final int FRAME_HEIGHT = 400;
   // Size of the text area
   private static final int ROWS = 13;  
   private static final int COLMS = 35;

   // Instance variables
   private Invoice invoice; 
   private JTextField elementText; 
   private JTextArea feedback; 
    
   /**
    * Constructs GUI
    * 
    */
   public InvoiceEditor()
   { 
     // Initialize instance variables
     invoice = new Invoice();
     elementText = new JTextField(COLMS);
     feedback = new JTextArea("",ROWS,COLMS);

  
     // Create a frame and a panel to GUI components 
     JFrame frame = new JFrame();
     JPanel panel = new JPanel();

     // Create buttons and add them to the panel   
     JButton addbutton = new JButton("Add");
     addbutton.addActionListener(new AddListener());
     panel.add(addbutton);
     
     JButton removebutton = new JButton("Remove");
     removebutton.addActionListener(new RemoveListener());
     panel.add(removebutton);
     
     JButton replacebutton = new JButton("Replace");
     replacebutton.addActionListener(new ReplaceListener());
     panel.add(replacebutton);
     
     JButton undobutton = new JButton("Undo");
     undobutton.addActionListener(new UndoListener());
     panel.add(undobutton);
     
     JButton movebutton = new JButton("Move Cursor");
     movebutton.addActionListener(new MoveCursorToListener());
     panel.add(movebutton);
     

     //Add the text field to the panel
     panel.add(elementText);
     
     //Add the text area to the panel
     panel.add(feedback);

     //Add the panel to the frame and configure the frame.
     frame.add(panel);
     frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
     frame.setResizable(false);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   } 

   // Write private Listener classes
   /**
    * LISTENER CLASSES
    */
   private class MoveCursorToListener implements ActionListener
   {
       public void actionPerformed(ActionEvent event)
       {
          String text = elementText.getText();
          if(text.isEmpty())
          {
             JOptionPane.showMessageDialog(null, "Please insert text.");
          }
          else
          {
             try
             {
                int cursor = Integer.parseInt(text);
                invoice.changeCursorTo(cursor);
             }
             catch(NumberFormatException e)
             {
                JOptionPane.showMessageDialog(null, "Please insert an integer.");
             }
             
          }
          updateFields();
       }
   }

   private class AddListener implements ActionListener
   {
       public void actionPerformed(ActionEvent event)
       {
          String text = elementText.getText();
          if(text.isEmpty())
          {
             JOptionPane.showMessageDialog(null, "Please insert text.");
          }
          else
          {
             try
             {
                String data[] = text.split(";");
                LineItem line = new LineItem(data[0], Double.parseDouble(data[1]));
                invoice.add(line);
             }
             catch(NumberFormatException e)
             {
                JOptionPane.showMessageDialog(null, "Format is name;price.");
             }
             catch(ArrayIndexOutOfBoundsException e)
             {
                JOptionPane.showMessageDialog(null, "Please check your format.");
             }
          }
          updateFields();
       }
   }

   private class RemoveListener implements ActionListener
   {
       public void actionPerformed(ActionEvent event)
       {
          String text = elementText.getText();
          if(text.isEmpty())
          {
             JOptionPane.showMessageDialog(null, "TextField is empty");
          }
          else
          {
             try
             {
                String data[] = text.split(";");
                invoice.remove(Integer.parseInt(data[0]), Integer.parseInt(data[1]));
             }
             catch(NumberFormatException e)
             {
                JOptionPane.showMessageDialog(null, "Format is integer;integer.");
             }
             catch(ArrayIndexOutOfBoundsException e)
             {
                JOptionPane.showMessageDialog(null, "Please check your format.");
             }
          }
          updateFields();
       }
   }

   private class ReplaceListener implements ActionListener
   {
          public void actionPerformed(ActionEvent event)
          {
             String text = elementText.getText();
             if(text.isEmpty())
             {
                JOptionPane.showMessageDialog(null, "TextField is empty.");
             }
             else
             {
                try
                {
                   String data[] = text.split("&");
                   String olditem[] = data[0].split(";");
                   LineItem oldline = new LineItem(olditem[0],Double.parseDouble(olditem[1]));
                   
                   String newitem[] = data[1].split(";");
                   LineItem newline = new LineItem(newitem[0],Double.parseDouble(newitem[1]));
                   
                   invoice.replace(oldline, newline);
                }
                catch(NumberFormatException e)
                {
                   JOptionPane.showMessageDialog(null, "Format is name;price&name;price.");
                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                   JOptionPane.showMessageDialog(null, "Please check your format.");
                }
             }
             updateFields();
          }
   }

   

   private class UndoListener implements ActionListener
   {
          public void actionPerformed(ActionEvent event)
          {
             try
             {
                invoice.undo();
             }
             catch(NullPointerException e)
             {
                JOptionPane.showMessageDialog(null, "There is no previous action.");
             }
             updateFields();
          }
   }

   // private method that displays the current text in the text area 
   // and flushes the text field.  
   private void updateFields()
   { 
     feedback.setText (invoice.toString());
     elementText.setText ("");
     elementText.requestFocus();
   }

  // main method to run this application
  public static void main (String [] args)
  {  new InvoiceEditor(); }
} // InvoiceEditor 
